package org.example.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.VO.OrderVO;
import org.example.entity.Customer;
import org.example.entity.Orders;
import org.example.mapper.OrdersMapper;
import org.example.service.ICustomerService;
import org.example.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Autowired
    private ICustomerService customerService;

    @Override
    public PageResult<Orders> selectByPage(int pageNum, int pageSize) {
        Page<Orders> page = this.page(new Page<>(pageNum, pageSize));
        return new PageResult<>(page.getRecords(), page.getTotal());
    }

    @Override
    public PageResult<Orders> search(int pageNum, int pageSize, OrderVO orderVO) {
        // TODO: 完成分页 + 条件查询
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(orderVO.getProductName()), "product_name", orderVO.getProductName())
                .gt(orderVO.getPrice() > 0, "price", orderVO.getPrice())
                .eq(orderVO.getCustomerid() != null, "customerid", orderVO.getCustomerid());

        Page<Orders> page = this.page(new Page<>(pageNum, pageSize), queryWrapper);

        page.getRecords().forEach(order -> {
            Customer customer = customerService.getById(order.getCustomerid());

        });


        return new PageResult<>(page.getRecords(), page.getTotal());
    }

    @Override
    public boolean addOrder(Orders orders) {
        return this.save(orders);
    }
}
